package io.zw.modules.xinpai.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.zw.common.validator.ValidatorUtils;
import io.zw.common.validator.group.AddGroup;
import io.zw.modules.sys.controller.AbstractController;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.zw.modules.xinpai.entity.CarEntity;
import io.zw.modules.xinpai.service.CarService;
import io.zw.common.utils.PageUtils;
import io.zw.common.utils.R;



/**
 * 
 *
 * @author zhuwei
 * @email 121203654@qq.com
 * @date 2018-10-10 13:15:04
 */
@RestController
@RequestMapping("xinpai/car")
public class CarController extends AbstractController{
    @Autowired
    private CarService carService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("xinpai:car:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("firm_id",getDeptId());
        PageUtils page = carService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{cId}")
    @RequiresPermissions("xinpai:car:info")
    public R info(@PathVariable("cId") Long cId){
        CarEntity car = carService.selectOne(new EntityWrapper<CarEntity>().eq("imei",cId).eq("firm_id",getDeptId()));
        //获取用户所属的区域列表
        if(car!=null){
            List<Long> arorList=carService.queryAreaList(car.getImei());
            car.setAreaList(arorList);
            return R.ok().put("car", car);
        }else{
            return R.error("非法查询");
        }


    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("xinpai:car:save")
    public R save(@RequestBody CarEntity car){
        ValidatorUtils.validateEntity(car, AddGroup.class);
        car.setState(0);
        car.setFirmId(getDeptId());
        carService.save(car);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("xinpai:car:update")
    public R update(@RequestBody CarEntity car){
        ValidatorUtils.validateEntity(car, AddGroup.class);
        if(car.getFirmId().equals(getDeptId())){
            carService.update(car);//全部更新
            return R.ok();
        }else{
            return R.error("非法更新");
        }

        

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("xinpai:car:delete")
    public R delete(@RequestBody Long[] cIds){
        carService.deleteBatchIds(Arrays.asList(cIds));
        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/select")
    @RequiresPermissions("xinpai:car:list")
    public R select(@RequestParam Map<String, String> params){
        List<CarEntity> list=carService.selectList(new EntityWrapper<CarEntity>().eq("firm_id",getDeptId())
                .like(StringUtils.isNotBlank(params.get("name")),"position_id",params.get("name"), SqlLike.DEFAULT)
                .eq(StringUtils.isNotBlank(params.get("state")),"state",params.get("state"))
                .eq(StringUtils.isNotBlank(params.get("projectId")),"project_id",params.get("projectId"))
        );
        if(params.get("nexus")!=null&&params.get("nexus").equals("true")){
            for (int i = 0; i < list.size(); i++) {
                CarEntity carEntity =  list.get(i);
                carEntity.setAreaList(carService.queryAreaList(carEntity.getImei()));
            }
        }
        return R.ok().put("list",list);
    }



}
